/**
 * 根据二维码进入
 */
import { RouterInfo } from '@tarojs/taro'
import { isDef } from '@/utils/utils'
import { ShopType } from './declare'
import Taro from '@tarojs/taro'
import { DesktopOrderType } from '@/utils/constants';
import { observable } from 'mobx';

type SceneDesktopElement = {
    scene?: string // 场景值
    desktopNo: string // 座位号
    path: string // path
    clientId?: string
}


const ARGS_NAME = 'tb'

export class SceneDesktopManager {

    // 桌号
    @observable
    private currentSceneDesktopEle: SceneDesktopElement


    // 订单类型
    @observable
    public orderType: string

    // 商铺类型
    private shopType: ShopType

    // previewUrl 下一步
    private previewUrl: string

    constructor() {
        this.currentSceneDesktopEle = {
            path: '/',
            desktopNo: '0'
        }
        this.orderType = DesktopOrderType[0];
    }

    getDesktopNo() {
        return this.currentSceneDesktopEle.desktopNo
    }

    setShopType(shopType: ShopType) {
        this.shopType = shopType
    }

    setOrderType(orderType: string) {
        this.orderType = orderType
    }
    setEle(router: RouterInfo) {
        if (isDef(router.params) && isDef(router.params[ARGS_NAME])) {
            this.currentSceneDesktopEle = {
                desktopNo: router.params[ARGS_NAME],
                path: router.path
            }
            // 有桌号，直接设置堂食
            if (this.currentSceneDesktopEle.desktopNo !== '0') {
                this.setOrderType(DesktopOrderType[0]);
            }

        }
    }
    setScene(scene: string) {
        if (isDef(this.currentSceneDesktopEle) && isDef(scene)) {
            this.currentSceneDesktopEle.scene = scene
        }
    }

    scanTable() {
        Taro.scanCode({
            onlyFromCamera: true,
            success: (res) => {
                const { errMsg, path } = res;
                if (errMsg === 'scanCode:ok') {
                    sceneDesktopManager.setOrderType(DesktopOrderType[0]);
                    const pathList = path.split('?');
                    if(pathList.length === 2 && pathList[0] === 'pages/goods/entry/index') {
                        Taro.navigateTo({ url: `/pages/checklist/checklist/index?${pathList[1]}` });
                    }
                }
            }
        });
    }


    async next(nextTodo?: (inst: SceneDesktopManager) => Promise<boolean>) {
        // TODO
        // 根据类型做调整
        // 没有桌号 怎么办
        // 首页记录 怎么走
        // if(isDef(this.getDesktopNo())){
        //     return DesktopOrderType.DESKTOP
        // }
        //正常店铺
        if (this.shopType === ShopType.OTHER) {
            //正常店铺的验证交给 nextTodo 返回Promise
            if (isDef(nextTodo) && typeof nextTodo === 'function') {
                const hasNext = await nextTodo(this)
                if (this.previewUrl && hasNext) {
                    return Taro.redirectTo({ url: this.previewUrl })
                }
                return hasNext
            }
            return true
        } else {
            // 餐饮
            // ----> 有桌号
            if (isDef(this.orderType)) {
                if (this.orderType === DesktopOrderType[0]) {
                    if (this.getDesktopNo() !== '0') {
                        if (this.previewUrl) {
                            Taro.redirectTo({
                                url: this.previewUrl
                            })
                        }
                        return true
                    } else {
                        this.scanTable();
                        return false
                    }
                } else if (this.orderType === DesktopOrderType[1]) {
                    if (isDef(nextTodo) && typeof nextTodo === 'function') {
                        return await nextTodo(this)
                    }
                    return true
                } else {
                    if (isDef(nextTodo) && typeof nextTodo === 'function') {
                        return await nextTodo(this)
                    }
                    // 自提不支持
                    return true
                }

            }
            // 没有订单类型 应该去弹框 这个应该交给nextTodo 去做
            if (isDef(nextTodo) && typeof nextTodo === 'function') {
                const hasNext = await nextTodo(this)

                if (hasNext && this.previewUrl) {
                    Taro.redirectTo({
                        url: this.previewUrl
                    })
                }
            }
            return false
        }
    }

}

export const sceneDesktopManager = new SceneDesktopManager()